package org.hive.vmware.client.ui;

import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.user.client.Element;

public class MainPage extends ContentPanel {
	
	private ConfigurationPanel configurationPanel;
	private TabItem configTab;
	private TabItem browseVMsTab;
	private TabItem groupVMTab;
	private BrowseVMsPanel browseVMsPanel;
	private GroupVMPanel groupVMPanel;

	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		setLayout(new FlowLayout(10));
		
		TabPanel folder = new TabPanel();
		folder.setWidth(725);
		folder.setAutoHeight(true);
		
		browseVMsTab = new TabItem("Browse VMs");
		browseVMsPanel = new BrowseVMsPanel();
		browseVMsTab.add(browseVMsPanel);
		folder.add(browseVMsTab);

		groupVMTab = new TabItem("Group VMs");
		groupVMPanel = new GroupVMPanel();
		groupVMTab.add(groupVMPanel);
		folder.add(groupVMTab);
		
		configTab = new TabItem("Manage Data Centers");
		configurationPanel = new ConfigurationPanel();
		configTab.add(configurationPanel);
		folder.add(configTab); 
		add(folder);
		registerConfigTabSelectionListener();
		registerGroupVMPanelTabSelectionListener();
		registerBrowseVMsTabSelectionListener();
		
	}
	
	void registerConfigTabSelectionListener() {
		configTab.addListener(Events.Select, new Listener<ComponentEvent>(){
			@Override
			public void handleEvent(ComponentEvent be) {
				configurationPanel.loadModel();
			}
		});
	}
	
	void registerBrowseVMsTabSelectionListener() {
		browseVMsTab.addListener(Events.Select, new Listener<ComponentEvent>(){
			@Override
			public void handleEvent(ComponentEvent be) {
				browseVMsPanel.loadModel();
			}
		});
	}
	
	void registerGroupVMPanelTabSelectionListener() {
		groupVMTab.addListener(Events.Select, new Listener<ComponentEvent>(){
			@Override
			public void handleEvent(ComponentEvent be) {
				groupVMPanel.loadModel();
			}
		});
	}
}
